package com.ex.mzwqz.adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ex.mzwqz.AppContext;
import com.ex.mzwqz.R;
import com.ex.mzwqz.model.NewsEntity;

/**
 * Created by xiaguoqiang on 16/10/19.
 */
public class NewsAdapter extends ArrayAdapter<NewsEntity> {

    private int resource;
    private Context mContext;

    public NewsAdapter(Context context, int resource) {
        super(context, resource);
        this.resource = resource;
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (vh==null){
            convertView =   LayoutInflater.from(mContext).inflate(resource,null);
            vh = new ViewHolder();
            vh.ivNews = (ImageView) convertView.findViewById(R.id.iv_news_pic);
            vh.tvTitle = (TextView) convertView.findViewById(R.id.tv_news_title);
            vh.tvDate = (TextView) convertView.findViewById(R.id.tv_news_date);
            vh.tvCategory = (TextView) convertView.findViewById(R.id.tv_news_type);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        NewsEntity news = getItem(position);
        vh.tvTitle.setText(news.getTitle());
        vh.tvDate.setText(news.getTime());
        vh.tvCategory.setText(news.getIntroduction());
//        AppContext.getInstance().getImageLoader().setDefaultDrawable(R.drawable.img_news_default);
        AppContext.getInstance().getImageLoader().get(news.getImgUrl(),vh.ivNews,mContext.getResources().getDrawable(R.drawable.img_news_default));
        return convertView;
    }

    class ViewHolder {
        ImageView ivNews;
        TextView tvTitle;
        TextView tvDate;
        TextView tvCategory;
    }
}
